<?php
$this->breadcrumbs=array(
	'Categories'=>array('index'),
	'Create',
);

$this->menu=array(
	array('label'=>'List Category', 'url'=>array('index')),
	array('label'=>'Manage Category', 'url'=>array('admin')),
);

$this->widget('ext.box.colorbox.colorboxWidget');
?>

<script type="text/javascript">
<!--
	$(function(){

		$("#cate_submit").button();
        $("#category-list-items").load('<?php echo $this->createUrl('category/items',array('type'=>Category::CATEGORY_PRODUCT)); ?>')

 	});


    
    function categoryCreate(){
            
            if($("#Category_cate_name").val()==''){                
                
                $.msgbox({
                    width:250,
                    height:100,
                    content:{type:'alert', content:'分类名称不能为空'},
                    animation:4
                });   
            }else{

            	var url = '<?php echo $this->createUrl('category'); ?>';

            	var data = {
					'Category[cate_name]':$("#Category_cate_name").val(),
					'Category[cate_type]':$("#Category_cate_type").val(),
					'Category[cate_pid]':$("#Category_cate_pid").val(),
					'Category[cate_type]':'<?php echo Category::CATEGORY_PRODUCT; ?>',
    				'Category[cate_order]':$("#Category_cate_order").val()==''?500:$("#Category_cate_order").val(),
    	    		'Category[cate_des]':$("#Category_cate_des").val()==''?'无':$("#Category_cate_des").val(),
    	    		'Category[cate_recommend]':$("#Category_cate_recommend").val(),
    	    		'Category[cate_contype]':$("#Category_cate_contype").val(),
    	    		'Category[cate_link]':$("#Category_cate_link").val(),
    	    		'self':1
            	};            	
             
				$.post(url,data,function(msg){
	                    if(msg == "fail"){
	                        message = "增加分类失败！";
	                    }else if(msg == "exists"){
	                        message = "该分类已经存在！";
	                    }else{
							message = "分类增加成功！";
	                    }
	    
	    				if(msg == "fail" || msg == "exists"){
	    				     $.msgbox({
	                            height:100,
	                            width:250,
	                            content:{type:'alert', content: message},
	                            animation:4
	                        });
	    					
	    				}else{    				    
	                        $("#category-list-items")
	                        .animate({
	                            opacity:0.5,
	                            'background-color':'yellow'
	                        
	                        },500)
	                        .html(msg)
	                        .load('<?php echo $this->createUrl('category/items',array('type'=>Category::CATEGORY_PRODUCT,'rid'=>time())); ?>')
	                        .animate({
	                            'background-color':'white',
	                            opacity:1
	                        
	                        },500);

	                        
	    				}
				});
              
            }
        

            return false;	
 	}   

 	function actionUpdate(object){
		var url = object.attr("id");

		$.colorbox({
			iframe:true, 
			innerWidth:425, 
			innerHeight:520,
			href:url,
			onClosed:function(){
				loadCategoryList();
			}
		});
 	}
    
    function quickShow(object){
//        object.parent().parent().siblings('.showitem').slideToggle();
        var item = object.parent().parent().siblings('.showitem').slideToggle(function(){
			if($(this).parent().height() == 100){
				$(this).parent().animate({height:20},1);
			}else{
				$(this).parent().animate({height:100},1);
			}
        });

    }    
    
    function quickModifyItem(object){
        
        
        var id = object.attr('id');
        
        var name = $("#cate_name_"+id).val();
        
		var contype = $("#cate_contype_"+id).val();			
			
		var objectparent = object.parent().parent().parent().parent().parent();
        var quick = objectparent.prev().find('.quick-cate-name');
        
//        alert(quick.html()+"\n"+id+"\n"+name+"\n"+contype);
        
		$.get('<?php echo $this->createUrl('category/quickupdate');?>',{"id":id,'name':name,'contype':contype},function(msg){
			if(msg == 'ok'){
				quick.text(name);
				objectparent.fadeOut();
					
			}
//			alert(msg);
		});
    }
    
    function deleteItem(object){
		var url = object.attr("id");

	     $.msgbox({
             width:250,
             height:100,
             content:{type:'confirm', content:'您确实到删除该分类？'},
             animation:4,
             onClose:function(v){
                 if(v){
					$.post(url,{'ajax':1},function(msg){
						
			
						loadCategoryList();
						if(id == 'ok'){
							
//							object.parent().parent().parent().parent().css({"background-color":"yellow"}).fadeOut();
						}
						
					});
                 }
             }
         });

    }

    function checkAll(object){
		$("input[name=category]").each(function(){
			if($(object).attr("checked") == true){
				$(this).attr("checked",true);
			}else{
				$(this).attr("checked",false);
			}
		});
    }
    
 	function deleteItems(){
		var str = "";
		$("input[name=category]").each(function(){
			if($(this).attr("checked") == true){
				str += ","+$(this).val();
			}
		});
		
		str = str.substring(1,str.length);
		
		$.msgbox({
            width:250,
            height:100,
            content:{type:'confirm', content:'确定要删除'+str+'分类'},
            animation:4,
            onClose:function(v){
                if(v){
					str = str.split(',');
					for(i=0;i<str.length;i++){
//						alert(str[i]);
						url = '<?php echo $this->createUrl('category/deleteremote'); ?>';
						url = url.replace(/<?php echo Yii::app()->urlManager->urlSuffix; ?>/,"/id/"+str[i]+"<?php echo Yii::app()->urlManager->urlSuffix; ?>");
						$.post(url,{ajax:1},function(msg){
						     $.msgbox({
					             width:250,
					             height:100,
					             content:{type:'alert', content:'您已经成功删除该分类。'},
					             animation:4
					         });
						     loadCategoryList();
						
						});
					}
                }
            }
		});
		

	}

	function pageselectCallback(page_index, jq){
		
		//var new_content = jQuery('#hiddenresult div.result:eq('+page_index+')').clone();

		
		
		jQuery.post('<?php echo $this->createUrl('category/hideresult',array('type'=>Category::CATEGORY_PRODUCT,'rn'=>rand()));?>',{page:page_index,catename:jQuery("#Category_cate_name").val()},function(data){
//			alert(data);
//			$("#hideResult").html(data);
			$("#Category_cate_name").val('');
			jQuery("#category-list").html(data);
			
		});

//		var new_content = $("#hideResult").html().clone();
//		
//		$('#category-list').empty().append(new_content);
		return false;
	}

    function loadCategoryList(){

//    	alert(jQuery(".current").text());
		jQuery.post('<?php echo $this->createUrl('category/hideresult',array('type'=>Category::CATEGORY_PRODUCT,'rn'=>rand()));?>',{page:jQuery(".current").text()},function(data){
//			alert(data);
//			$("#hideResult").html(data);
			$("#Category_cate_name").val('');
			jQuery("#category-list").html(data);
			
		});
        
        $("#category-list-items")
	        .animate({
	            opacity:0.5,
	            'background-color':'yellow'
	        
	        },500)
//	        .html(msg)
	        .load('<?php echo $this->createUrl('category/items',array('type'=>Category::CATEGORY_PRODUCT,'rid'=>time())); ?>')
	        .animate({
	            'background-color':'white',
	            opacity:1
	        
	        },500);        
		
    }


//-->
</script>

<div class="column1">
	<div class="box">
		<?php $this->beginWidget('zii.widgets.CPortlet', array('title'=>CHtml::image($this->module->registerImage('article.png'),'Logo', array('width'=>'20')) .'&nbsp;&nbsp;Create News Category',	));?>
		   	<div class="box-content">
				<?php echo $this->renderPartial('/news/_formcategory', array('model'=>$model)); ?>
			</div>
		<?php $this->endWidget();?>
	</div>
</div>
<div class="column2">
     <div class="box">
 		<?php $this->beginWidget('zii.widgets.CPortlet', array('title'=>CHtml::image($this->module->registerImage('article.png'),'Logo', array('width'=>'20')) .'&nbsp;&nbsp;Product Category List',	));?>
	     	<div class="box-content" id="category-list-items">
	     	
	     	</div>
	     <?php $this->endWidget();?>
     </div>
	
</div>
